package com.shengqi.xinnongren.work.ui.home.fragments;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpFragment;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.utils.ImageUtils;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.home.model.MyCenterBean;
import com.shengqi.xinnongren.work.ui.home.presenter.HomePage5Presenter;
import com.shengqi.xinnongren.work.ui.home.view.HomePage5View;
import com.shengqi.xinnongren.work.ui.user.activities.AddressActivity;
import com.shengqi.xinnongren.work.ui.user.activities.DailySignActivity;
import com.shengqi.xinnongren.work.ui.user.activities.LoginActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MemberInfoActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyAfterSaleListActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyCollectionActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyCommentActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyCouponActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyIntegralActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyOrderListActivity;
import com.shengqi.xinnongren.work.ui.user.activities.MyWalletActivity;
import com.shengqi.xinnongren.work.ui.user.activities.ServiceActivity;
import com.shengqi.xinnongren.work.ui.user.activities.SettingActivity;
import com.shengqi.xinnongren.work.ui.user.activities.ToBeVipActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页->我的
 * A simple {@link Fragment} subclass.
 */
public class HomePage5Fragment extends BaseMvpFragment<HomePage5View, HomePage5Presenter> implements HomePage5View {

    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_balance)
    TextView tv_balance;
    @BindView(R.id.tv_integral)
    TextView tv_integral;
    @BindView(R.id.tv_my_coupon)
    TextView tv_my_coupon;
    @BindView(R.id.iv_advertisement)
    ImageView iv_advertisement;
    @BindView(R.id.tv_order1)
    TextView tv_order1;
    @BindView(R.id.tv_order2)
    TextView tv_order2;
    @BindView(R.id.tv_order3)
    TextView tv_order3;
    @BindView(R.id.tv_order4)
    TextView tv_order4;

    @BindView(R.id.ll_vip_tag_container)
    LinearLayout mLlVipTagContainer;
    @BindView(R.id.tv_vip_invalidate_time)
    TextView mTvVipInvalidateTime;


    @BindView(R.id.sr_refresh)
    SwipeRefreshLayout mRefreshLayout;
    private MyCenterBean mMyCenterBean;

    public HomePage5Fragment() {
        // Required empty public constructor
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_home_page5;
    }


    @Override
    protected void lazyLoadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    protected void onVisibilityChanged(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            getUserInfo();
        }
    }

    private void getUserInfo() {
        if (UserManager.getInstance().isLogin())
            getPresenter().getMyCenterPage();
        else
            mRefreshLayout.setRefreshing(false);
    }

    @Override
    protected void initView() {
        mRefreshLayout.setOnRefreshListener(this::getUserInfo);
    }

    @Override
    protected void initData() {

    }


    /**
     * 登录
     */
    @OnClick(R.id.tv_name)
    void login() {
        if (!Global.checkLogin(getContext())) {
            startActivity(new Intent(getContext(), LoginActivity.class));
        }
    }

    /**
     * 待支付
     */
    @OnClick(R.id.ll_wait_pay)
    void ll_wait_pay() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 1);
        }
    }

    /**
     * 待发货
     */
    @OnClick(R.id.ll_wait_take)
    void ll_wait_take() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 2);
        }
    }

    /**
     * 待收货
     */
    @OnClick(R.id.ll_wait_receive)
    void ll_wait_receive() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 3);
        }
    }

    /**
     * 待评论
     */
    @OnClick(R.id.ll_wait_comment)
    void ll_wait_comment() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 4);
        }
    }

    /**
     * 查看所有订单
     */
    @OnClick(R.id.tv_browse_all_order)
    void tv_browse_all_order() {
        if (Global.checkLogin(getContext())) {
            MyOrderListActivity.start(getContext(), 0);
        }
    }

    /**
     * 我的余额
     */
    @OnClick(R.id.ll_my_wallet)
    void ll_my_wallet() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyWalletActivity.class));
    }

    /**
     * 我的积分
     */
    @OnClick(R.id.ll_my_integral)
    void ll_my_integral() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyIntegralActivity.class));
    }

    /**
     * 基本信息
     */
    @OnClick({R.id.ll_base_info, R.id.iv_head})
    void ll_base_info() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MemberInfoActivity.class));
    }

    /**
     * 收货地址
     */
    @OnClick(R.id.ll_address)
    void ll_address() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), AddressActivity.class));
    }

    /**
     * 我的售后
     */
    @OnClick(R.id.ll_after_sales_service)
    void ll_after_sales_service() {
//        if (mMyCenterBean == null || mMyCenterBean.myAfterSale == null) {
//            return;
//        }
        if (Global.checkLogin(getContext()))
//            new AfterSalesDialog.Builder(getContext())
//                    .setData(
//                            mMyCenterBean.myAfterSale.SHHeadUrl,
//                            mMyCenterBean.myAfterSale.SHName,
//                            mMyCenterBean.myAfterSale.SHPhone,
//                            mMyCenterBean.myAfterSale.SHContent
//                    ).build().show();
            startActivity(new Intent(getContext(), MyAfterSaleListActivity.class));
    }

    /**
     * 积分商城
     */
    @OnClick(R.id.ll_integral_store)
    void ll_integral_store() {
        if (Global.checkLogin(getContext()))
            Toast.makeText(getContext(), "积分商城", Toast.LENGTH_SHORT).show();
    }

    /**
     * 成为会员
     */
    @OnClick(R.id.ll_become_vip)
    void ll_become_vip() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), ToBeVipActivity.class));
    }

    /**
     * 我的评价
     */
    @OnClick(R.id.ll_my_comment)
    void ll_my_comment() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyCommentActivity.class));
    }

    /**
     * 我的优惠券
     */
    @OnClick(R.id.ll_my_coupon)
    void ll_my_coupon() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyCouponActivity.class));
    }

    /**
     * 我的收藏
     */
    @OnClick(R.id.ll_my_collection)
    void ll_my_collection() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), MyCollectionActivity.class));
    }

    /**
     * 联系客服
     */
    @OnClick(R.id.ll_contact_us)
    void ll_contact_us() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), ServiceActivity.class));
    }

    /**
     * 签到
     */
    @OnClick(R.id.ll_daily_sign)
    void ll_daily_sign() {
        if (Global.checkLogin(getContext()))
            startActivity(new Intent(getContext(), DailySignActivity.class));
    }

    /**
     * 设置
     */
    @OnClick(R.id.iv_setting)
    void iv_setting() {
        startActivity(new Intent(getContext(), SettingActivity.class));
    }

    @Override
    protected HomePage5Presenter createPresenter() {
        return new HomePage5Presenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
        mRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMyCenterPage(MyCenterBean bean) {
        mRefreshLayout.setRefreshing(false);
        mMyCenterBean = bean;
        ImageUtils.showImage(getContext(), bean.getHeadUrl(), iv_head);
        List<MyCenterBean.AdvertisingBean> advertising = bean.getAdvertising();
        if (advertising.size() > 0) {
            iv_advertisement.setVisibility(View.VISIBLE);
            ImageUtils.showImage(getContext(), advertising.get(0).getPicUrl(), iv_advertisement);
        } else
            iv_advertisement.setVisibility(View.GONE);
        tv_name.setText(bean.getNickName());
        if (bean.getIsVip() == 1) {
            mLlVipTagContainer.setVisibility(View.VISIBLE);
            mTvVipInvalidateTime.setText(String.format("到期时间：%s", bean.getVipEndTime()));
        }
        tv_balance.setText(bean.getBalance());
        tv_integral.setText(bean.getIntegral());
        tv_my_coupon.setText(bean.getCouponCount());
        if (bean.getObligationNum() > 0) {
            tv_order1.setVisibility(View.VISIBLE);
            tv_order1.setText(String.valueOf(bean.getObligationNum()));
        }
        if (bean.getWaitSendNum() > 0) {
            tv_order2.setVisibility(View.VISIBLE);
            tv_order2.setText(String.valueOf(bean.getWaitSendNum()));
        }
        if (bean.getWaitDeliveryNum() > 0) {
            tv_order3.setVisibility(View.VISIBLE);
            tv_order3.setText(String.valueOf(bean.getWaitDeliveryNum()));
        }
        if (bean.getWaitEvaluateNum() > 0) {
            tv_order4.setVisibility(View.VISIBLE);
            tv_order4.setText(String.valueOf(bean.getWaitEvaluateNum()));
        }

        /*} else if (respond.result == -1) {
            Global.reLogin(getActivity());
        } else
            ToastHelper.getInstance(getContext()).showWarn(respond.message);*/

    }

    @Override
    public void onTokenInvalid() {
        resetViewStyle();
    }

    private void resetViewStyle() {
        mLlVipTagContainer.setVisibility(View.GONE);
        tv_name.setText(R.string.str_no_login);
        tv_balance.setText(R.string.str_empty_money);
        tv_integral.setText(R.string.str_zero);
        tv_my_coupon.setText(R.string.str_zero);
        tv_order1.setVisibility(View.GONE);
        tv_order2.setVisibility(View.GONE);
        tv_order3.setVisibility(View.GONE);
        tv_order4.setVisibility(View.GONE);
    }
}
